//
//  RunHistoryController.swift
//  MoyoungHome
//
//  Created by 易先生 on 2022/9/29.
//

import UIKit
import RxCocoa
import MYModule
import RxDataSources
import RxSwift

open class TrainingRecordsController: ViewController {

	public let selectIdRelay = PublishRelay<Int>()
    
    public let emptyView = LottieEmptyView()
    
    open var viewModel: TrainingRecordsViewModel = TrainingRecordsViewModel() {
        
        didSet {
            
            tableView.reloadData()
            emptyView.isHidden = !viewModel.sections.isEmpty
            
        }
        
    }

	public var isLookMore: Bool = false

	open override func viewDidLoad() {
		super.viewDidLoad()
		
        setNavigationBar()

	}
    
    open func setNavigationBar() {
        
        navigation.bar.titleTextAttributes = [.foregroundColor: UIColor.assist_222222]
        navigation.bar.tintColor = UIColor.assist_222222
        navigation.bar.backgroundColor = UIColor.darkBackground
        navigation.item.title = "training_records_title".language
        
    }

	open override func makeUI() {
		super.makeUI()
        
        self.view.backgroundColor =  UIColor.darkBackground
        
        emptyView.isHidden = !viewModel.sections.isEmpty
        
        tableView.backgroundColor = self.view.backgroundColor
        tableView.separatorStyle = .singleLine
        tableView.separatorColor = UIColor.assist_D0D0D0
        tableView.separatorInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
        
		view.addSubview(tableView)
        view.addSubview(emptyView)
        
		tableView.snp.makeConstraints { make in
			make.edges.equalToSuperview()
		}
        
        emptyView.snp.makeConstraints { make in
            make.center.equalTo(tableView.snp.center)
        }
	}

	open override func bindViewModel() {
		super.bindViewModel()
        
       
		
	}
    
    //MARK: - lazy load -
	private lazy var tableView: TableView = {
		let tableView = TableView(frame: .zero, style: .grouped)
		tableView.register(TrainingRecordsTableViewCell.self, forCellReuseIdentifier: TrainingRecordsTableViewCell.reuseIdentifier)
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        tableView.estimatedSectionHeaderHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.estimatedRowHeight = 100
        
        tableView.dataSource = self
        tableView.delegate = self

		return tableView
	}()

}

extension TrainingRecordsController: UITableViewDataSource {

    public func numberOfSections(in tableView: UITableView) -> Int {
        return viewModel.sections.count
    }
    
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.sections[section].items.count
    }
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
        let item = viewModel.sections[indexPath.section].items[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: TrainingRecordsTableViewCell.reuseIdentifier) as! TrainingRecordsTableViewCell
        cell.bind(item)
        return cell
    }
}

extension TrainingRecordsController: UITableViewDelegate{
    
    public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let title = viewModel.sections[section].title
        let sectionView = View(frame: CGRect(x: 0, y: 0, width: view.bounds.size.width, height: 48))
        sectionView.backgroundColor = self.view.backgroundColor
        let sectionTitleLbael = Label(frame: CGRect(x: 20, y: 16, width: view.bounds.size.width-40, height: 16))
        sectionTitleLbael.font = Fonts.systemFont(ofSize: 14)
        sectionTitleLbael.textColor = UIColor.assist_A9A9A9
        sectionTitleLbael.text = title
        sectionView.addSubview(sectionTitleLbael)
        return sectionView
    }
    
    public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 40
    }
    
    public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.001
    }
    
    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        
        let section = viewModel.sections[indexPath.section]
        let model = section.items[indexPath.row]
        
        guard let type = TrainingType(rawValue: model.type) else {
            return
        }
        
        if type.isGPS {
            if model.locations.count == 0 {
                let vc = TrainingDetailViewController()
                vc.showRightItem = false
                vc.viewModel = TrainingDetailViewModel(trainModel: model)
                self.navigationController?.pushViewController(vc, animated: true)
                return
            }
            let vc = GPSTrainingRecordTableViewController()
            vc.type = type
            let route = model
            vc.gpsModel = route
            self.navigationController?.pushViewController(vc, animated: true)
        } else {
            let vc = TrainingDetailViewController()
            vc.showRightItem = false
            vc.viewModel = TrainingDetailViewModel(trainModel: model)
            
            self.navigationController?.pushViewController(vc, animated: true)
            
            
        }
        
    }
    
    
}

